﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using System.Web.Configuration;

public partial class Admin_MembershipProvider_Access_access_rule_summary : System.Web.UI.Page
{
    private const string VirtualImageRoot = "~/";
    private string selectedRole, selectedUser;

    public void Page_Init()
    {
        UserRoles.DataSource = System.Web.Security.Roles.GetAllRoles();
        UserRoles.DataBind();

        UserList.DataSource = Membership.GetAllUsers();
        UserList.DataBind();

        FolderTree.Nodes.Clear();
    }

    public void Page_Load()
    {
        selectedRole = UserRoles.SelectedValue;
        selectedUser = UserList.SelectedValue;
    }

    public void Page_PreRender()
    {
    }

    public void PopulateTree(string byUserOrRole)
    {
       
        DirectoryInfo rootFolder = new DirectoryInfo(Server.MapPath(VirtualImageRoot));
        TreeNode root = AddNodeAndDescendents(byUserOrRole, rootFolder, null);
        FolderTree.Nodes.Add(root);
    }

    public TreeNode AddNodeAndDescendents(string byUserOrRole, DirectoryInfo folder, TreeNode parentNode)
    {
       
        string virtualFolderPath;
        if (parentNode == null)
        {
            virtualFolderPath = VirtualImageRoot;
        }
        else
        {
            virtualFolderPath = parentNode.Value + folder.Name + "/";
        }

       
        Configuration config = WebConfigurationManager.OpenWebConfiguration(virtualFolderPath);
        SystemWebSectionGroup systemWeb = (SystemWebSectionGroup)config.GetSectionGroup("system.web");
        AuthorizationSection section = (AuthorizationSection)systemWeb.Sections["authorization"];

        string action;
        if (byUserOrRole == "ByRole")
        {
            action = GetTheRuleForThisRole(section, virtualFolderPath);
        }
        else if (byUserOrRole == "ByUser")
        {
            action = GetTheRuleForThisUser(section, virtualFolderPath);
        }
        else
        {
            action = "";
        }

        
        TreeNode node = new TreeNode(folder.Name + " (" + action + ")", virtualFolderPath);
        node.ImageUrl = (action.Substring(0, 5) == "ALLOW") ? "/Coupons/Admin/MembershipProvider/Access/i/greenlight.gif" : "/Coupons/Admin/MembershipProvider/Access/i/redlight.gif";
        node.NavigateUrl = "access_rules.aspx?selectedFolderName=" + folder.Name;

       
        DirectoryInfo[] subFolders = folder.GetDirectories();
        foreach (DirectoryInfo subFolder in subFolders)
        {
          
            if (subFolder.Name != "_controls" && subFolder.Name != "App_Data")
            {
                TreeNode child = AddNodeAndDescendents(byUserOrRole, subFolder, node);
                node.ChildNodes.Add(child);
            }
        }
        return node; 
    }

   

    public string GetTheRuleForThisRole(AuthorizationSection section, string folder)
    {
        

        foreach (AuthorizationRule rule in section.Rules)
        {
           
            foreach (string user in rule.Users)
            {
                if (user == "*")
                {
                    return rule.Action.ToString().ToUpper() + ": All Users";
                }
            }
            foreach (string role in rule.Roles)
            {
                if (role == selectedRole)
                {
                    return rule.Action.ToString().ToUpper() + ": Role=" + role;
                }
            }
        }
       
        return "Allow";
    }

    public string GetTheRuleForThisUser(AuthorizationSection section, string folder)
    {
        foreach (AuthorizationRule rule in section.Rules)
        {
            foreach (string user in rule.Users)
            {
                if (user == "*")
                {
                    return rule.Action.ToString().ToUpper() + ": All Users";
                }
                else if (user == selectedUser)
                {
                    return rule.Action.ToString().ToUpper() + ": User=" + user;
                }
            }

            // Don't forget that users might belong to some roles!
            foreach (string role in rule.Roles)
            {
                if (System.Web.Security.Roles.IsUserInRole(selectedUser, role))
                {
                    return rule.Action.ToString().ToUpper() + ": Role=" + role;
                }
            }
        }
        return "ALLOW";
    }

    public void DisplayRoleSummary(object sender, EventArgs e)
    {
        FolderTree.Nodes.Clear();
        UserList.SelectedIndex = 0;
        if (UserRoles.SelectedIndex > 0)
        {
            PopulateTree("ByRole");
            FolderTree.ExpandAll();
        }
    }

    public void DisplayUserSummary(object sender, EventArgs e)
    {
        FolderTree.Nodes.Clear();
        UserRoles.SelectedIndex = 0;
        if (UserList.SelectedIndex > 0)
        {
            PopulateTree("ByUser");
            FolderTree.ExpandAll();
        }
    }

    public void DisplaySecuritySummary(object sender, TreeNodeEventArgs e)
    {
        e.Node.ShowCheckBox = true;
    }

    public void FolderTree_SelectedNodeChanged(object sender, EventArgs e)
    {
    }
}
